﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lcmyjobs.Web.Admin
{
    public partial class ListJobsCheck : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["AdminLogin2"] != null)
                {
                    int atid = Convert.ToInt32(Session["AdminLogin2"]);

                    Bind(atid);
                }
                else
                {
                    Maticsoft.Common.MessageBox.ResponseScript(this, "alert('请先登陆！');top.location.href='adminlogin2.aspx'");
                }
            }
        }

        protected void lbn_check_Click(object sender, EventArgs e)
        {
            string Idlist = this.GetSelIDlist();

            if (!string.IsNullOrEmpty(Idlist))
            {
                lcmyjobs.BLL.enterprise_releasejob eb = new BLL.enterprise_releasejob();

                if (eb.UpdateWhere("status", "1", Idlist))
                {
                    new lcmyjobs.BLL.enterprise_askhunter().UpdateWhere("status", "1", Idlist);
                    //Maticsoft.Common.MessageBox.Show(this, "审核成功！");
                    if (Session["AdminLogin2"] != null)
                    {
                        //if (ViewState["page"] != null)
                        //    AspNetPager1.CurrentPageIndex = int.Parse(ViewState["page"].ToString());

                        int atid = Convert.ToInt32(Session["AdminLogin2"]);

                        Bind(atid);
                    }

                }
            }
            else
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "请选择所要审核的单位！", "ListJobsCheck.aspx");
            }
        }

        protected void lbn_up_Click(object sender, EventArgs e)
        {
            string Idlist = this.GetSelIDlist();

            if (!string.IsNullOrEmpty(Idlist))
            {
                lcmyjobs.BLL.enterprise_releasejob eb = new BLL.enterprise_releasejob();

                if (eb.UpdateWhere("status","2",Idlist))
                {
                    //Maticsoft.Common.MessageBox.Show(this, "取消审核成功！");
                    if (Session["AdminLogin2"] != null)
                    {
                        //if (ViewState["page"] != null)
                        //    AspNetPager1.CurrentPageIndex = int.Parse(ViewState["page"].ToString());

                        int atid = Convert.ToInt32(Session["AdminLogin2"]);

                        Bind(atid);
                    }
                }
            }
            else
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "请选择所要审核的单位！", "ListJobsCheck.aspx");
            }
        }

        protected void lbn_del_Click(object sender, EventArgs e)
        {
            string Idlist = this.GetSelIDlist();

            if (!string.IsNullOrEmpty(Idlist))
            {
                lcmyjobs.BLL.enterprise_releasejob eb = new BLL.enterprise_releasejob();

                string[] ids = Idlist.Split(',');
                bool flag = false;

                foreach (string id in ids)
                {
                    if (eb.Delete(Convert.ToInt32(id)))
                    {
                        flag = true;
                    }
                    else
                    {
                        flag = false;
                    }
                }

                if (flag)
                {
                    //if (ViewState["page"] != null)
                    //    AspNetPager1.CurrentPageIndex = int.Parse(ViewState["page"].ToString());

                    //Maticsoft.Common.MessageBox.Show(this, "删除成功！");

                    if (Session["AdminLogin2"] != null)
                    {
                        int atid = Convert.ToInt32(Session["AdminLogin2"]);

                        Bind(atid);

                    }
                    
                }
            }
            else
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "请选择所要删除的职位！", "ListJobsCheck.aspx");
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            if (Session["AdminLogin2"] != null)
            {         
                int atid = Convert.ToInt32(Session["AdminLogin2"]);

                Bind(atid);

                //ViewState["page"] = AspNetPager1.CurrentPageIndex;

            }
        }

        private void Bind(int atid)
        {
            //if (ViewState["page"] != null && ViewState["page"].ToString() != "")
            //    AspNetPager1.CurrentPageIndex = int.Parse(ViewState["page"].ToString());

            int count = 0;

            DataSet ds = new lcmyjobs.BLL.enterprise_releasejob().GetReleaseJobByStatus(atid,0, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out count);

            this.lbl_count.Text = count.ToString();

            int recordCount = 0;

            if (ds.Tables[1].Rows.Count > 0)
            {
                recordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            }

            this.AspNetPager1.RecordCount = recordCount;

            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
        }

        /// <summary>
        /// 获取主键ID列表
        /// </summary>
        /// <returns></returns>
        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)this.GridView1.Rows[i].FindControl("cbo_rid");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (GridView1.DataKeys[i].Value != null)
                    {
                        idlist += GridView1.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells[6].Text == "未审核")
            {
                e.Row.Cells[6].ControlStyle.ForeColor = System.Drawing.Color.Red;
            }
        }


        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbn_check")
            {
                if (e.CommandArgument != null)
                {

                    if (new lcmyjobs.BLL.enterprise_askhunter().UpdateWhere("hunter_time", DateTime.Now.ToString(), e.CommandArgument.ToString()))
                    {
                        //Maticsoft.Common.MessageBox.Show(this, "刷新猎头成功！");
                        if (Session["AdminLogin2"] != null)
                        {
                            int atid = Convert.ToInt32(Session["AdminLogin2"]);

                            Bind(atid);
                        }

                    }

                }
            }
        }

        //成功
        protected void lbn_issuccess_Click(object sender, EventArgs e)
        {
            string idList = this.GetSelIDlist();

            if (!string.IsNullOrEmpty(idList))
            {
                if (new lcmyjobs.BLL.enterprise_askhunter().UpdateWhere("issuccess", "1", idList))
                {
                    //Maticsoft.Common.MessageBox.Show(this, "成功！");
                    if (Session["AdminLogin2"] != null)
                    {
                        int atid = Convert.ToInt32(Session["AdminLogin2"]);

                        Bind(atid);
                    }
                }

            }
            else
            {
                Maticsoft.Common.MessageBox.Show(this, "请选择所要成功的单位！");
            }
        }

        protected void lbn_refresh_Click(object sender, EventArgs e)
        {
            string idList = this.GetSelIDlist();

            if (!string.IsNullOrEmpty(idList))
            {
                if (new lcmyjobs.BLL.enterprise_askhunter().UpdateWhere("hunter_time", DateTime.Now.ToString(), idList))
                {
                    //Maticsoft.Common.MessageBox.Show(this, "刷新猎头成功！");
                    if (Session["AdminLogin2"] != null)
                    {
                        int atid = Convert.ToInt32(Session["AdminLogin2"]);

                        Bind(atid);
                    }
                }

            }
            else
            {
                Maticsoft.Common.MessageBox.Show(this, "请选择所要刷新猎头的单位！");
            }
        }

        public string GetBH(string str)
        {
            if (str == null || str=="")
            {
                return "";
            }
            return "L" + str.ToString().PadLeft(6, '0');
        }
    }
}